package com.maszy.servlet;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Action;
import com.maszy.service.ActionService;
import com.maszy.service.CashRegisterService;
import com.maszy.service.DoorStoreService;
import com.maszy.service.RptService;


public class RptServlet  extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	ActionService service = new ActionService();
	String view = new String(); // 页面路径
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		
		RptServlet servlet = new RptServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");		
		if ("queryAll".equals(method)) {
			// 查询销售明细
			servlet.queryAll(request, response);
			view = "/background/rpt/rptTable.jsp";
		} 
		else if ("queryAllForward".equals(method)) {
			// 进入查询页面			
			servlet.queryAllForward(request, response);
			view = "/background/rpt/rptMain.jsp";
		} else if ("queryRptBySaleNO".equals(method)) {
			// 根据流水号查询详细记录
			servlet.queryRptBySaleNO(request, response);			
			view = "/background/rpt/rptItem.jsp";
		} 

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void queryAllForward(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		DoorStoreService dss=new DoorStoreService();
		List dssList=dss.queryAll();
		request.setAttribute("dssList",dssList);
		CashRegisterService crs=new CashRegisterService();
		List crsList=crs.queryAll();
		request.setAttribute("crsList", crsList);
	}



	//根据流水号查询
	private void queryRptBySaleNO(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String saleNo=request.getParameter("saleNo");
		String AmtAdd=request.getParameter("AmtAdd");
		RptService service=new RptService();
		List list1=service.queryRptBySaleNO(saleNo);
		request.setAttribute("list1", list1);
		request.setAttribute("AmtAdd", AmtAdd);
		List list2=service.queryRptPayBySaleNO(saleNo);
		request.setAttribute("list2", list2);
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		Map m=new HashMap();
		String doorStore_Id=request.getParameter("DoorStore_Id");
		String cashRegister_Id=request.getParameter("CashRegister_Id");
		String time=request.getParameter("time");
		if(!"".equals(cashRegister_Id)&&cashRegister_Id!="0"){
			//查询一台收银机
			m.put("cashRegister_Id", cashRegister_Id);
		}else{
			m.put("doorStore_Id", doorStore_Id);
		}
		m.put("time", time);
		RptService service=new RptService();		
		List list=service.queryRpt(m);
		if(list.size()>0){
			Double d=service.queryRptSum(m);
			request.setAttribute("d", d);
		}
	
		request.setAttribute("list", list);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Action obj = new Action();
		String id=request.getParameter("Action_Id");
		if(!"".equals(id)&&id!=null){		
			obj.setAction_Id(Long.parseLong(id));		
			obj.setAction_Name(request.getParameter("Action_Name"));
			obj.setAction_Remark(request.getParameter("Action_Remark"));
			service.add(obj);
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}